// pages/autocar/salecar.js
var util = require('../../utils/util.js');
const app = getApp();
var clickflag = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listInfo: [],
    bannerUrl: '',
    keepItems: [],
    date: "2016-09-01",
    isshow: true,
    showModalAuth: false,
    animationData: '',
    isLoginAuth: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!app.globalData.isLoginAuth) {
      this.showModal();
    }
    this.setData({
      isLoginAuth: app.globalData.isLoginAuth
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var date = new Date();
    var nowdate=util.formatDate(date);
    this.setData({
      listInfo: app.globalData.listInfo,
      date: nowdate
    })
    this.initBanner();
  },
  
  //广告图片初始化
  initBanner: function () {
    var that = this;
    var requrl = app.globalData.hostUrl + 'common/banner';
    var imgbanner = wx.getStorageSync('sellBanner');
    //console.log(imgbanner.length);
    if (!app.expiratTime('sellBanner') || imgbanner.length == 0) {
      wx.request({
        url: requrl,
        data: { 'location': '304' },
        header: {
          'grant-appid': app.globalData.appid
        },
        success: function (res) {
          if (res.statusCode === 200) {
            wx.setStorageSync('sellBanner', res.data.result[0].imgpath);
            that.setData({
              bannerUrl: res.data.result[0].imgpath
            })
          }
        }
      })
    }
    else {
      that.setData({
        bannerUrl: imgbanner
      })
    }
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  formSubmit: function (e) {
    var that = this;
    var postdata = {
      phone: e.detail.value.phone,
      name: e.detail.value.name,
      banner: e.detail.value.banner,
      gls: e.detail.value.gls,
      regtime: that.data.date,
      addr: e.detail.value.addr,
      uid: wx.getStorageSync('userId'),
      'openid': wx.getStorageSync('openId'),
      'formid': e.detail.formId
    };
    // console.log(postdata);
    if (clickflag ==1){
          clickflag=0;
          wx.request({
            url: app.globalData.hostUrl + 'handler/sellorder',
            data: postdata,
            header: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'grant-appid': app.globalData.appid
            },
            method: 'POST',
            success: function (res) {
      //        console.log(res);
              if (res.data.code == 200) {
                wx.showToast({
                  title: '预约成功',
                  icon: 'success',
                  duration: 2000
                })
                wx.showModal({
                  title: '提示',
                  content: '预约成功,请您等待销售顾问联系!',
                  success: function (res) {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '../autocar/index'
                      })
                    }
                  }
                });
              }
              else {
                wx.showToast({
                  title: '预约失败',
                  icon: 'error',
                  duration: 2000
                })
              }
              clickflag = 1;
            }
          })
    }
  },
  gohome: function () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  yuy: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/thappointment',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myappointment',
      })
    }
  },
  orders: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/trorder',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myorder',
      })
    }
  },
  mys: function () {
    wx.redirectTo({
      url: '../myself/myself',
    })
  },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease-in-out",
      delay: 0
    })
    this.animation = animation
    animation.translateY(500).step()
    this.setData({
      animationData: animation.export(),
      showModalAuth: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      showModalAuth: false,
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e);
    if (e.detail.errMsg == 'getUserInfo:ok') {
      app.globalData.userInfo = e.detail.userInfo;
      app.getfansinfo();
    }
    else if (e.detail.errMsg == "getUserInfo:fail auth deny") {

    }
    else {
      wx.redirectTo({
        url: '../xuecar/index',
      })
    }
  }
})